package com.dhcc.bpm.business.papc.controller;

import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.common.utils.ExcelUtil;
import com.dhcc.bpm.common.utils.http.HttpsUtils;
import io.swagger.annotations.Api;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.business.papc.config.BusinessConfig;
import com.dhcc.bpm.business.papc.entity.FtpInfoConfig;
import com.dhcc.bpm.business.papc.service.IFtpInfoConfigService;
import com.dhcc.bpm.business.papc.vo.FtpInfoConfigVO;
import org.springblade.core.boot.ctrl.BladeController;
import springfox.documentation.annotations.ApiIgnore;
import java.util.List;
import java.util.Map;

/**
 * FTP参数表  控制器
 *
 * @author liuzhao
 * @since 2020-05-16
 */
@RestController
@AllArgsConstructor
@RequestMapping("bpm-papc/ftpinfo")
@Api(value = "FTP参数表 ", tags = "FTP参数表 接口")
public class FtpInfoConfigController extends BladeController {
    private final static Logger log = LoggerFactory.getLogger(FtpInfoConfigController.class);
    @Autowired
    private BusinessConfig busConfig;
    private IFtpInfoConfigService ftpInfoCondfigService;

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入ftpInfoConfig")
    public R<FtpInfoConfig> detail(FtpInfoConfig ftpInfoConfig) {
//        FtpInfo detail = ftpInfoService.getOne(Condition.getQueryWrapper(ftpInfo));
    	FtpInfoConfig detail = ftpInfoCondfigService.selectFtpInfoConfigById(ftpInfoConfig.getId());
        return R.data(detail);
    }

    /**
     * 分页 FTP参数表
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页", notes = "传入ftpInfoConfig")
    @ApiLog("FTP参数表")
    @Log(title = "FTP参数表", businessType = BusinessType.SELECT)
    public R<IPage<FtpInfoConfig>> list(@ApiIgnore @RequestParam Map<String, Object> ftpInfoConfig, Query query) {
        IPage<FtpInfoConfig> pages = ftpInfoCondfigService.page(Condition.getPage(query), Condition.getQueryWrapper(ftpInfoConfig, FtpInfoConfig.class));
        return R.data(pages);
    }
   /**
    * 刷新redis
    * @return
    */
	@ApiLog("刷新redis")
	@PostMapping("/refush")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "刷新redis", notes = "传入")
	@Log(title = "刷新redis", businessType = BusinessType.REFRESH)
	public R refush() {
		try {
			String tableName="ZjywPrdtInfo";
			String url=busConfig.getBusConfig().getBusurl();
            String response = HttpsUtils.sendSSLPostBodyRequest(url,tableName);
            JSONObject jsons = JSONObject.parseObject(response);
            int retcode=(int) jsons.get("status");
            if(200==retcode) {
				log.info("刷新配置成功!");
				return R.status(true);
			}else {
				log.error("刷新配置失败");
				return R.status(false);
			}
		}catch (Exception e){
			log.error("刷新失败:{}", e);
			return R.status(false);
		}
	}
    /**
     * 自定义分页 FTP参数表
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页", notes = "传入ftpInfoConfig")
    public R<IPage<FtpInfoConfigVO>> page(FtpInfoConfigVO ftpInfo, Query query) {
        IPage<FtpInfoConfigVO> pages = ftpInfoCondfigService.selectFtpInfoConfigPage(Condition.getPage(query), ftpInfo);
        return R.data(pages);
    }

    /**
     * 新增 FTP参数表
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "传入ftpInfoConfig")
    public R save(@Valid @RequestBody FtpInfoConfig ftpInfoConfig) {
        return R.status(ftpInfoCondfigService.save(ftpInfoConfig));
    }

    /**
     * 修改 FTP参数表
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入ftpInfoConfig")
    public R update(@Valid @RequestBody FtpInfoConfig ftpInfoConfig) {
        return R.status(ftpInfoCondfigService.updateById(ftpInfoConfig));
    }

    /**
     * 新增或修改 FTP参数表
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增或修改", notes = "传入ftpInfoConfig")
    public R submit(@Valid @RequestBody FtpInfoConfig ftpInfoConfig) {
        return R.status(ftpInfoCondfigService.saveOrUpdate(ftpInfoConfig));
    }


    /**
     * 删除 FTP参数表
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "逻辑删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
//        return R.status(ftpInfoService.deleteLogic(Func.toLongList(ids)));
        return R.status(ftpInfoCondfigService.deleteFtpInfoConfigByIds(ids));
    }

    @PostMapping("/listFtpInfoToExcel")
    public void listFtpInfoToExcel(FtpInfoConfig ftpInfoConfig, HttpServletResponse response) {
        List<FtpInfoConfig> ftpInfoList = ftpInfoCondfigService.selectFtpInfoConfigAll(ftpInfoConfig);
        ExcelUtil<FtpInfoConfig> util = new ExcelUtil<FtpInfoConfig>(FtpInfoConfig.class);
        util.exportExcel(ftpInfoList, "FTP参数表", response);
    }
}
